<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/16-9:35
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\model\AdminUser;
    use app\admin\model\Settings;
    use app\admin\service\AdminUserService;
    use app\admin\service\RoleService;
    use app\admin\validate\Pass;
    use Jrk\Util;
    use think\Exception;


    class Setting extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = new Settings();


        }



        /**
         * @param RoleService $roleService
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/26
         * @name: index
         * @describe:
         */
        public function index()
        {
            if (IS_POST) {
                $data     = $this->request->post();
                $validate = new \app\admin\validate\AdminUser();
                if (!$validate->scene('edit')->check($data)) {
                    $this->error($validate->getError());
                }
                parent::addLog("修改管理员信息", "修改管理员信息:".$data['username']);
                //dump($data);die;
                $admin_user = new AdminUser();

                return $admin_user->addAndEdit($data);
            }
            $info        = AdminUserService::getOne($this->admin_id);
            $roleService = new RoleService();
            $role        = $roleService->getOne(['id' => $this->system_auth]);
            $role_name   = $role['name'];

            $this->assign(compact("info", 'role_name'));

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/26
         * @name: pass
         * @describe:修改密码
         */
        public function pass()
        {
            if (IS_POST) {
                $data = $this->request->post();

                $pass = Util::think_decrypt($this->online['password']);
                if ($pass != $data['oldpassword']) {
                    $this->error("当前密码输入错误");
                }

                $validate = new Pass();
                //校验数据
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                $data['password'] = Util::think_encrypt($data['password']);

                unset($data['oldpassword']);
                unset($data['repassword']);

                parent::addLog("修改管理员密码", "修改管理员密码");


                $result = AdminUser::update($data);
                if ($result) {
                   $this->success("修改成功");
                } else {
                    $this->error("修改失败");
                }

            }

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: safe
         * @describe:安全设置
         */
        public function safe()
        {
            if (IS_POST) {
                $data = $this->request->post();
                $res  = db("setting")->where("key", "safe")->setField("value", json_encode($data['safe']));
                parent::addLog("修改设置", "修改系统安全设置");
                $res ? $this->success("提交成功") : $this->error("提交失败");
            }
            $safe = $this->model->getSetting();
            $this->assign("safe", $safe['safe']);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: system
         * @describe:系统设置
         */
        public function system()
        {
            $safe = $this->model->getSetting();
            //网站设置
            $this->assign("info", $safe);
            //安全设置
            $this->assign("safe", $safe['safe']);

            //邮件设置
            $this->assign("mail", $safe['mail']);

            return $this->fetch();
        }


        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: emailPost
         * @describe:邮件设置
         */
        public function emailPost()
        {
            if (IS_POST) {
                $data = $this->request->post();
                try {
                    $res = db("setting")->where("key", "mail")->setField("value", json_encode($data['mail']));
                    parent::addLog("修改设置", "修改邮件设置");
                    $res ? $this->success("提交成功") : $this->error("提交失败");
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
        }


        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: sitePost
         * @describe:网站设置提交
         */
        public function sitePost()
        {
            if (IS_POST) {
                $data = $this->request->post();
                try {
                    foreach ($data as $k => $v) {
                        db("setting")->where("key", $k)->setField("value", $v);
                    }
                    $this->success("提交成功");
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/16
         * @name: alertSkin
         * @describe:皮肤动画设置
         */
        public function alertSkin()
        {

            return $this->fetch();
        }


    }